﻿using MaterialSkin;
using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TomatoClock.Models;
using TomatoClock.Services;

namespace TomatoClock.Views
{
    public partial class SelecteGroupForm : MaterialForm
    {
        public List<BanGroup> ForbiddeGroups;
        ProcessManager processManager;
        List<string> Groups =new List<string>();
        private readonly MaterialSkinManager materialSkinManager;
        private float x;//定义当前窗体的宽度
        private float y;
        public SelecteGroupForm(ProcessManager processManager)
        {   
            InitializeComponent();
            materialSkinManager = MaterialSkinManager.Instance; // 初始化 MaterialSkinManager 实例
            materialSkinManager.AddFormToManage(this);  // 将要应用 Material Design 的窗体添加到管理列表中
            materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;   // Theme 属性用来设置整体的主题
            materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE); // ColorScheme 属性来设置配色方案
            this.processManager = processManager;
            ForbiddeGroups = processManager.LoadUserGroups(processManager.UserId);
            foreach (var group in ForbiddeGroups)
            {
                Groups.Add(group.GroupName);
            }
        }
        public void updatematerialCheckedListBox1()
        {
            materialCheckedListBox1.Items.Clear();
            foreach (var group in Groups)
            {
                materialCheckedListBox1.Items.Add(group);
            }
        }
        public string GetSelectedGroupNames()
        {
            string selectedGroup = null;
            int selectedCount = 0;

            for (int i = 0; i < materialCheckedListBox1.Items.Count; i++)
            {
                if (materialCheckedListBox1.GetItemCheckState(i) == CheckState.Checked)
                {
                    selectedGroup = materialCheckedListBox1.Items[i].ToString();
                    selectedCount++;
                }
            }

            if (selectedCount == 1)
            {
                return selectedGroup;
            }
            else if (selectedCount == 0)
            {
                throw new InvalidOperationException("No group selected. Please select one group.");
            }
            else
            {
                throw new InvalidOperationException("Multiple groups selected. Please select only one group.");
            }
        }
        private void materialButton1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }
        private void materialButton3_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
        private void materialButton2_Click(object sender, EventArgs e)
        {
            updatematerialCheckedListBox1();
        }
    }   
}
